package service

import (
	//"emma/platform"
	//"fmt"
	"reflect"
	//"runtime"
)

type Method struct {
	Type  reflect.Method
	Value reflect.Value
	paramCount int
}

func newMethod(t reflect.Method, v reflect.Value, p int) *Method {
	result := new(Method)
	result.Type = t
	result.Value = v
	result.paramCount = p

	return result
}

func (method *Method) Name() string {
	return method.Type.Name
}

func (method *Method) ParamsCount() int {
	return method.paramCount
}

func (method *Method) Call(args []reflect.Value) (err error) {
	//defer func() {
	//	if r := recover(); err == nil && r != nil {
	//		buf := make([]byte, 0xffff)
	//		count := runtime.Stack(buf, false)
	//		buf = buf[:count]
	//		err = fmt.Errorf("call service failed, reason: %v%s%v",
	//			r,
	//			platform.EndLine(),
	//			string(buf),
	//		)
	//	}
	//}()

	results := method.Value.Call(args)
	if len(results) > 0 {
		if first, ok := results[0].Interface().(error); ok && first != nil {
			err = first
		}
	}

	return
}
